<?php
/**
 * @Author:      zk
 * @DateTime:    18-7-12 下午1:23
 * @Description: 用于资源的录入和 维护
 */
class OAResource extends Eloquent
{
    protected $table = 'oa_resource';

    /**
     * 资源的类型数组
     * @var array 
     */
    public static $typeArr = [
        1 => '目录',
        2 => '菜单',
        3 => '资源',
    ];
    
    /**
     * 状态的类型数组
     * @var array 
     */
    public static $statusArr = [
        1 => '开启',
        2 => '冻结',
        3 => '删除',
    ];
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-16 下午4:50
     * @Description: 根据资源类型、资源状态 获取相应的资源列表
     * @param int $type  资源类型 (1:一级目录;2:菜单;3:操作资源;)
     * @param array $status_arr 状态数组 [] (1:开启;2:冻结;3:删除;)
     * @param int $res_type 内部数据返回类型 [1:数组;2:对象;]
     * @param int $pid 资源父id [默认为空]
     * @return array
     */
    public static function getResourceList($type, $status_arr, $res_type = 1, $pid = '') {
        $result = [];
        if(!is_array($status_arr)){
            return $result;
        }
        $select_arr = [
            'id', 'url', 'name', 'en_name', 'pid', 'ext_id', 'type', 'status', 'sort', 'icon',
        ];
        $result = OAResource::where('type', $type)
                ->whereIn('status', $status_arr)
                ->select($select_arr)
                ->orderBy('sort')
                ->orderBy('id');
        if($pid != ''){
            $result = $result->where('pid', $pid)->get();
        } else {
            $result = $result->get();
        }
        if($res_type == 1){
            $result = $result->toArray();
        }
        return $result;
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-20 上午9:50
     * @Description: 根据用户id获取 该用户所拥有的资源信息
     * @param int $user_id      用户id
     * @return array
     */
//    public static function getResourceByUserid($user_id) {
//        $result = [
//            'resource_id_arr' => [],    //资源id数组
//            'ext_id_arr' => [],         //前端自定义id数组
//            'list_arr' => [],           //资源列表数组
//        ];
//        $select_arr = [
//            'oa_resource.id', 'oa_resource.url', 'oa_resource.name', 'oa_resource.en_name', 'oa_resource.pid', 
//            'oa_resource.ext_id', 'oa_resource.type', 'oa_resource.status', 'oa_resource.sort', 'oa_resource.icon',
//        ];
//        $list = OAResource::leftJoin('oa_role_resource', 'oa_resource.id', '=', 'oa_role_resource.resource_id')
//                ->leftJoin('oa_user_role', 'oa_role_resource.role_id', '=', 'oa_user_role.role_id')
//                ->where('oa_resource.status', 1)
//                ->where('oa_resource.type', 3)
//                ->where('oa_user_role.status', 1)
//                ->where('oa_user_role.user_id', $user_id)
//                ->select($select_arr)
//                ->orderBy('oa_resource.sort')
//                ->orderBy('oa_resource.id')
//                ->get()
//                ->toArray();
//        if(!empty($list)){
//            foreach ($list as $k => $v) {
//                $result['resource_id_arr'][] = $v['id'];
//                $result['ext_id_arr'][] = $v['ext_id'];
//                $result['list_arr'][] = $v;
//            }
//        }
//        return $result;
//    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-20 上午10:20
     * @Description: 根据用户id获取 该用户所拥有的菜单、目录、资源
     * @param int $user_id      用户id
     * @return array
     */
    public static function getMenuByUserid($user_id) {
        $result = [];
        $role_info = OAUserRole::getRoleInfoByUserId($user_id);
        if(empty($role_info)){
            return $result;
        }
        $role_id_arr = [];      //该用户的角色id数组
        foreach ($role_info as $k => $v) {
            $role_id_arr[] = $v['role_id'];
        }
 
        $select_arr = [
            'oa_resource.id', 'oa_resource.url', 'oa_resource.name', 'oa_resource.en_name', 'oa_resource.pid', 
            'oa_resource.ext_id', 'oa_resource.type', 'oa_resource.status', 'oa_resource.sort', 'oa_resource.icon',
        ];
        //type  资源类型 (1:一级目录;2:菜单;3:操作资源;)
        $list = OAResource::leftJoin('oa_role_resource', 'oa_resource.id', '=', 'oa_role_resource.resource_id')
                ->where('oa_resource.status', 1)
                ->where('oa_resource.type', 1)
                ->whereIn('oa_role_resource.role_id', $role_id_arr)
                ->select($select_arr)
                ->groupBy('oa_resource.id')
                ->orderBy('oa_resource.sort')
                ->get()
                ->toArray();
        if(!empty($list)){
            foreach ($list as $k => $v) {
                $temp = $v;
                //查找菜单下的目录
                $menu = OAResource::leftJoin('oa_role_resource', 'oa_resource.id', '=', 'oa_role_resource.resource_id')
                        ->where('oa_resource.status', 1)
                        ->where('oa_resource.type', 2)
                        ->where('oa_resource.pid', $v['id'])
                        ->whereIn('oa_role_resource.role_id', $role_id_arr)
                        ->select($select_arr)
                        ->groupBy('oa_resource.id')
                        ->orderBy('oa_resource.sort')
                        ->get()
                        ->toArray();
                if(!empty($menu)){
                    foreach ($menu as $k_2 => &$v_2) {
                        //获取目录下的资源
                        $resource = OAResource::leftJoin('oa_role_resource', 'oa_resource.id', '=', 'oa_role_resource.resource_id')
                                ->where('oa_resource.status', 1)
                                ->where('oa_resource.type', 3)
                                ->where('oa_resource.pid', $v_2['id'])
                                ->whereIn('oa_role_resource.role_id', $role_id_arr)
                                ->select($select_arr)
                                ->groupBy('oa_resource.id')
                                ->orderBy('oa_resource.sort')
                                ->get()
                                ->toArray();
                        if(!empty($resource)){
                            $v_2['resource'] = $resource;
                        }
                    }
                    $temp['menu'] = $menu;
                }
                $result[] = $temp;
            }
        }
        
        return $result;
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-8-20 下午2:11
     * @Description: 根据角色id获取 该角色所拥有的菜单、目录、资源
     * @param int $role_id      角色id
     * @return array
     */
    public static function getMenuByRoleId($role_id) {
        $result = [];
        $select_arr = [
            'oa_resource.id', 
//            'oa_resource.url', 'oa_resource.name', 'oa_resource.en_name', 'oa_resource.pid', 
//            'oa_resource.ext_id', 'oa_resource.type', 'oa_resource.status', 'oa_resource.sort', 'oa_resource.icon',
        ];
        //type  资源类型 (1:一级目录;2:菜单;3:操作资源;)
        $list = OAResource::leftJoin('oa_role_resource', 'oa_resource.id', '=', 'oa_role_resource.resource_id')
                ->where('oa_resource.status', 1)
                ->where('oa_resource.type', 1)
                ->where('oa_role_resource.role_id', $role_id)
                ->select($select_arr)
//                ->orderBy('oa_resource.sort')
                ->get()
                ->toArray();
        if(!empty($list)){
            foreach ($list as $k => $v) {
                $temp = $v;
                //查找菜单下的目录
                $menu = OAResource::leftJoin('oa_role_resource', 'oa_resource.id', '=', 'oa_role_resource.resource_id')
                        ->where('oa_resource.status', 1)
                        ->where('oa_resource.type', 2)
                        ->where('oa_resource.pid', $v['id'])
                        ->where('oa_role_resource.role_id', $role_id)
                        ->select($select_arr)
//                        ->orderBy('oa_resource.sort')
                        ->get()
                        ->toArray();
                if(!empty($menu)){
                    foreach ($menu as $k_2 => &$v_2) {
                        //获取目录下的资源
                        $resource = OAResource::leftJoin('oa_role_resource', 'oa_resource.id', '=', 'oa_role_resource.resource_id')
                                ->where('oa_resource.status', 1)
                                ->where('oa_resource.type', 3)
                                ->where('oa_resource.pid', $v_2['id'])
                                ->where('oa_role_resource.role_id', $role_id)
                                ->select($select_arr)
//                                ->orderBy('oa_resource.sort')
                                ->get()
                                ->toArray();
                        if(!empty($resource)){
                            $v_2['resource'] = $resource;
                        }
                    }
                    $temp['menu'] = $menu;
                }
                $result[] = $temp;
            }
        }
        
        return $result;
    }
    
}